<?php

namespace Api;

use Illuminate\Support\Facades\DB;

/*
|--------------------------------------------------------------------------
| 特性：Artisan 命令拓展
|--------------------------------------------------------------------------
|
*/

trait CommandExtTrait
{
    /**
     * 在类中添加__get()方法，在直接获取属性值时自动调用一次，以属性名作为参数传入并处理
     * @param $propertyName
     *
     * @return int
     */
    public function __get($propertyName)
    {  
        if (preg_match('/Model$/', $propertyName)) {
            return DB::table($this->Models[$propertyName]);
        }
        else {
            return $this->$propertyName;
        }
    }

    // /**
    //  * 校验账号当前数据是否正常
    //  *
    //  * @return void
    //  * 
    //  * @throws \Member\AccountCheck\Exceptions\Exception
    //  */
    // public function check($personId) {
    //     // 当前数据
    //     $person = $this->PersonModel
    //         ->where('id', $personId)
    //         ->lockForUpdate()
    //         ->first();
        
    //     // 校对数据
    //     $checkData = $this->Model->where('member_person_id', $personId)->first();

    //     if ($checkData) {
    //         // 计算
    //         $coinFail       = 0 + $person->coin        !== $checkData->before_coin        + $checkData->change_coin;
    //         $walletFail     = 0 + $person->wallet      !== $checkData->before_wallet      + $checkData->change_wallet;
    //         $frozenCoinFail = 0 + $person->frozen_coin !== $checkData->before_frozen_coin + $checkData->change_frozen_coin;

    //         if ($coinFail || $walletFail || $frozenCoinFail) {
    //             // 创建待处理的 管理员修正记录
    //             $count = $this->AccountCorrectionModel->where('member_person_id', $personId)->where('status', 1)->count();
    //             if ($count === 0) {
    //                 // 事务回滚
    //                 DB::rollBack();
    //                 // 创建记录
    //                 $correctionLog = $this->AccountCorrectionModel;
    //                 $correctionLog->member_person_id = $personId;
    //                 $correctionLog->status    = 1;
    //                 $correctionLog->saveOrFail();
    //             }
    //             // 抛出异常
    //             throw_exception('Member\AccountCheck', 'CheckFail', '区块校验中', '账户数据异常');
    //         }
    //     }
    // }

    // /**
    //  * 新增常规操作记录
    //  *
    //  * @return void
    //  * 
    //  * @throws \Member\AccountCheck\Exceptions\Exception
    //  */
    // public function log($modelName, $modelId, $description, $personId, $before, $change) {

    //     // 变更记录
    //     $changeLog = $this->AccountChangeModel;

    //     $changeLog->operation_model       = $modelName;
    //     $changeLog->member_operation_id          = $modelId;
    //     $changeLog->operation_description = $description;
    //     $changeLog->type                  = 1;
    //     $changeLog->member_person_id             = $personId;

    //     isset($before['coin'])        && $changeLog->before_coin        = $before['coin'];
    //     isset($before['wallet'])      && $changeLog->before_wallet      = $before['wallet'];
    //     isset($before['frozen_coin']) && $changeLog->before_frozen_coin = $before['frozen_coin'];

    //     isset($change['coin'])        && $changeLog->change_coin        = $change['coin'];
    //     isset($change['wallet'])      && $changeLog->change_wallet      = $change['wallet'];
    //     isset($change['frozen_coin']) && $changeLog->change_frozen_coin = $change['frozen_coin'];

    //     $changeLog->saveOrFail();
        

    //     // 校验记录（不存在创建，存在则更新）
    //     $checkLog = $this->Model->where('member_person_id', $personId)->first() ?: $this->Model;

    //     $checkLog->operation_model       = $modelName;
    //     $checkLog->member_operation_id          = $modelId;
    //     $checkLog->operation_description = $description;
    //     $checkLog->member_person_id             = $personId;

    //     isset($before['coin'])        && $checkLog->before_coin        = $before['coin'];
    //     isset($before['wallet'])      && $checkLog->before_wallet      = $before['wallet'];
    //     isset($before['frozen_coin']) && $checkLog->before_frozen_coin = $before['frozen_coin'];

    //     isset($change['coin'])        && $checkLog->change_coin        = $change['coin'];
    //     isset($change['wallet'])      && $checkLog->change_wallet      = $change['wallet'];
    //     isset($change['frozen_coin']) && $checkLog->change_frozen_coin = $change['frozen_coin'];

    //     $checkLog->saveOrFail();
    // }


}
